﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eTerm.PlugIn.NCore;
using System.Net.Mail;
using eTerm.PlugIn.TicketImport;
using System.IO;

namespace eTerm.PlugIn.TicketImport
{
    public class ExcelImportPlugIn:BaseSocketPlugIn,IPlugIn
    {
        private PnrDetr DetrNode { get; set; }
        private Queue<PnrDetr> _tslItems = new Queue<PnrDetr>();
        private frmImport frm = null;
        private List<PnrDetr> _ReceiverItems = new List<PnrDetr>();
        /// <summary>
        /// 差件名称
        /// </summary>
        /// <value>用于日志记录的插件名称.</value>
        public override string PlugInName
        {
            get { return @"票号EXCEL导入检查插件"; }
        }


        /// <summary>
        /// 插件授权码.
        /// </summary>
        /// <value>The plug in code.</value>
        protected override int PlugInCode
        {
            get { return 128; }
        }

        protected override void OpenDatabase(string ProviderName, string DbString)
        {
            DbString = string.Format(@"Data Source={0};Version=3;New=False;Connection Timeout=600;",Path.Combine(AssemblyPath, DbString));
            Context.Instance.DbProvider = new SqlDbProvider(DbString, ProviderName);
        }

        protected override string SetupXmlFile
        {
            get
            {
                return @"ExcelPlugIn.xml";
            }
        }


        /// <summary>
        /// 连接认证成功，开始指令序列.
        /// </summary>
        protected override void StartQueue()
        {
    //        PnrDetr.All()
    //            .Where(l => l.StatusId == (int)eTerm.MsSql.DbProvider.DetrStatus.PENDDING || ((l.StatusId & (int)eTerm.MsSql.DbProvider.DetrStatus.OPENFORUSE) != 0 /*&& l.ProcessDate <= DateTime.Now.AddDays(-CheckFrequency)*/))
    //            /*
    //            .Where(l =>
    //(l.StatusId == (int)DetrStatus.PENDDING || (l.StatusId & ((int)DetrStatus.PENDDING | (int)DetrStatus.OPENFORUSE)) != 0)
    //&&
    //(/*l.TicketDate == null || l.ProcessDate <= DateTime.Now.AddDays(-CheckFrequency) || l.TicketDate <= DateTime.Now.AddDays(-CheckFrequency))*/
    //        .ToList().ForEach(l => { _tslItems.Enqueue(l); });
    //        Dequeue();
            frm = new frmImport();
            frm.FormClosed += (o, e) => {
                //FireOnExecuted();
                DisposePlugIn();
            };
            frm.ShowDialog();
        }

        protected override void DisposePlugIn()
        {
            base.DisposePlugIn();
            if (frm == null) {
                frm.Dispose();
            }
        }


        /// <summary>
        /// 顺序处理票号.
        /// </summary>
        private void Dequeue()
        {
            DetrNode = _tslItems.Count > 0 ? _tslItems.Dequeue() : null;
            if (DetrNode == null) { FireOnExecuted(); return; }
            DetrNode.LSTFK_PNR_SEGM_REFERENCE_PNR_DETR = new List<PnrSegment>();
            SendStream(string.Format(@"DETR:TN/{0}", DetrNode.TicketNo.Trim()));
        }

        /// <summary>
        /// 结束插件调用事件通知
        /// </summary>
        protected override void FireOnExecuted()
        {
            base.FireOnExecuted();
            //邮件通知人员
            _ReceiverItems.GroupBy(l => l.UserId, (Key, Items) => new { Key, Count = Items.Count(), Items }).ToList().ForEach(l =>
            {
                SendMail(l.Key ?? 0, l.Items.ToList());
            });
            _ReceiverItems.Clear();
        }

        private void SendMail(int UserId, List<PnrDetr> items)
        {
            StringBuilder sb = new StringBuilder(@"<Result>");
            try
            {
                SysUser uModel = SysUser.FirstOrDefault(u => u.UserId == UserId);
                sb.AppendFormat(@"<DisplayName>{0}</DisplayName>", uModel.DisplayName);
                sb.Append(@"<Tickets>");
                items.ForEach(l =>
                {
                    sb.Append(@"<Ticket>");
                    //l.LSTFK_PNR_SEGM_REFERENCE_PNR_DETR = l.LSTFK_PNR_SEGM_REFERENCE_PNR_DETR.Count == 0 ? PnrSegment.All().Where(s => s.TicketNoId == l.Id).ToList() : l.LSTFK_PNR_SEGM_REFERENCE_PNR_DETR;
                    sb.AppendFormat(@"<Id>{0}</Id>", l.Id);
                    sb.AppendFormat(@"<TicketDate>{0}</TicketDate>", (l.TicketDate ?? DateTime.Now).ToString(@"yyyy-MM-dd"));
                    sb.AppendFormat(@"<ProcessDate>{0}</ProcessDate>", l.ProcessDate.ToString(@"yyyy-MM-dd"));
                    sb.AppendFormat(@"<TicketNo>{0}</TicketNo>", l.TicketNo);
                    sb.AppendFormat(@"<DetrStatus><![CDATA[{0}]]></DetrStatus><Segments>", GetEnumName((eTerm.MsSql.DbProvider.DetrStatus)l.StatusId));
                    l.LSTFK_PNR_SEGM_REFERENCE_PNR_DETR.ToList().ForEach(s =>
                    {
                        sb.Append(@"<Segment>");

                        sb.AppendFormat(@"<Departure>{0}</Departure>
<Arrival>{1}</Arrival>
<FlightNo>{2}</FlightNo>
<CabinCode>{3}</CabinCode>
<DepartureDate>{4}</DepartureDate>
<StatusName>{5}</StatusName>
", s.Departure, s.Arrival, s.FlightNo, s.CabinCode, s.DepartureDate.HasValue ? s.DepartureDate.Value.ToString(@"yyyy-MM-dd") : string.Empty, GetEnumName((eTerm.MsSql.DbProvider.DetrStatus)s.StatusId));

                        sb.Append(@"</Segment>");
                    });
                    sb.Append(@"</Segments></Ticket>");
                });
                sb.Append(@"</Tickets>");
                //FireOnNotify(uModel.UserNo, EventType.INFO, string.Format(@"通知<{0}>个票号{1}", uModel.Email, items.Count));
                sb.Append(@"</Result>");
                SendEmail(sb.ToString(), new System.Net.Mail.MailAddressCollection() { new MailAddress(uModel.Email, uModel.DisplayName) }, new System.Net.Mail.MailAddressCollection() { }, new System.Net.Mail.MailAddressCollection() { new MailAddress(@"SHINSOFT@163.COM", @"开发助手") });
            }
            catch (Exception ex)
            {
                FireOnNotify(PlugInName, EventType.ERROR, ex.Message);
            }
        }

        /// <summary>
        /// 指令结果分析.
        /// </summary>
        /// <param name="PakcetName">指令名称.</param>
        /// <param name="PakcetResult">指令结果.</param>
        protected override void ExecutePacket(string PakcetName, string PakcetResult)
        {
            FireOnNotify(PakcetName, EventType.INFO, PakcetResult);


            DetrInfo di = new AnalysisDetr { DetrStr = PakcetResult }.ParseDetr();
            DateTime szDate = DateTime.Now.AddDays(-1);
            //SendStream(string.Format(@"TPR:{0}/{1}", 6, string.Format(@"{0}{1}", szDate.Day.ToString("D2"), getDateString(szDate))));
            if (di.FltSeg != null)
            {
                di.FltSeg.ToList().ForEach(l =>
                {
                    FireOnNotify(DetrNode.TicketNo, EventType.INFO, string.Format(@"发现航段：{0}{1}    {2}->{3}    {4}", l.Airline, l.FlightNo, l.Departure, l.Arrival, l.DepartureDate));
                });
            }
            PnrHistory history = new PnrHistory()
            {
                ProcessDate = DateTime.Now,
                TicketNoId = DetrNode.Id,
                SourceStatusId = DetrNode.StatusId,
            };
            DateTime? maxDateValue = null;
            if (di.FltSeg != null)
            {
                new PnrSegment().Delete(l => l.TicketNoId == DetrNode.Id);
                di.FltSeg.OrderBy(l => l.SegId).ToList().ForEach(l =>
                {
                    PnrSegment seg = new PnrSegment()
                    {
                        Arrival = l.Arrival,
                        CabinCode = l.Carbin,
                        Departure = l.Departure,
                        TicketNoId = DetrNode.Id,
                        IsInvalid = l.Status != eTerm.MsSql.DbProvider.DetrStatus.OPENFORUSE,
                        DepartureDate = maxDateValue,
                        StatusId = (int)l.Status,
                        FlightNo = string.Format(@"{0}{1}", l.Airline, l.FlightNo)
                    };
                    seg.Save();
                    if (DetrNode.StatusId == (int)eTerm.MsSql.DbProvider.DetrStatus.PENDDING)
                    {
                        DetrNode.StatusId = (int)di.Status;
                        //maxDateValue = getDateString(l.DepartureDate);
                        if (di.FltSeg.Length > 0 && DetrNode.TicketDate == null)
                            DetrNode.TicketDate = PnrSegment.All().Where(p => p.TicketNoId == DetrNode.Id).Select(p => p.DepartureDate).Max();
                    }
                    else
                    {
                        DetrNode.TicketDate = DateTime.Now.AddMonths(-1);
                    }
                    DetrNode.LSTFK_PNR_SEGM_REFERENCE_PNR_DETR.Add(seg);
                });
            }
            else if ((DetrNode.StatusId & (int)eTerm.MsSql.DbProvider.DetrStatus.PENDDING) != 0)
            {
                DetrNode.StatusId = DetrNode.StatusId | (int)di.Status; //di.FltSeg.ToList().Count(l => l.Status ==DetrStatus.OPENFORUSER) > 0 ? (int)DetrStatus.PENDDING : (int)DetrStatus.NOTEXIST;
            }
            //添加航段
            DetrNode.ProcessDate = DateTime.Now;
            DetrNode.StatusId = (int)di.Status;
            history.NewStatusId = DetrNode.StatusId;
            if (history.NewStatusId != history.SourceStatusId)
            {
                DetrNode.DetrDescription = PakcetResult;
                history.DetrInfo = PakcetResult;
                history.Save();
                _ReceiverItems.Add(DetrNode);
                FireOnNotify(DetrNode.TicketNo, EventType.SUCCESS, string.Format(@"票号[{0}]状态发生变更。", DetrNode.TicketNo));

            }
#if DEBUG
#else
            DetrNode.Save();
#endif



            Dequeue();
        }
    }
}
